package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.behavior.pojos.Relation;
import com.heima.model.search.vos.SearchArticleVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Psycho
 * @create 2022-04-27-18:45
 */
@Service
@Slf4j
@Transactional
public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {

    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;


    @Autowired
    private ApArticleMapper apArticleMapper;


    /**
     * 生成静态文件上传到minIO中
     *
     * @param apArticle
     * @param content
     */
    @Override
    public void buildArticleToMinIO(ApArticle apArticle, String content) {
        //2.文章内容通过freemarker生成html文件
        //freemarker的模板对象，获取模板
        Template template = null;
        try {
            template = configuration.getTemplate("article.ftl");
            //合成
            //第一个参数 数据模型
            JSONArray jsonArray = JSON.parseArray(content);
            Map<String, Object> map = new HashMap<>();
            map.put("content", jsonArray);

            //第二个参数  输出流
            StringWriter out = new StringWriter();
            template.process(map, out);
            //将输出流传入minio的输入流
            InputStream is = new ByteArrayInputStream(out.toString().getBytes());
            //3.把html文件上传到minio中
            String htmlLocation = fileStorageService.uploadHtmlFile("", apArticle.getId() + ".html", is);
            //4.修改ap_article表，保存static_url字段
            apArticle.setStaticUrl(htmlLocation);
            apArticleMapper.updateById(apArticle);

            //发送消息，创建索引
            createArticleESIndex(apArticle,content,htmlLocation);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Autowired
    private KafkaTemplate<String,Object> kafkaTemplate;


    /**
     * 索引的新建
     * @param apArticle
     * @param content
     * @param htmlLocation
     */
    private void createArticleESIndex(ApArticle apArticle, String content, String htmlLocation) {
        if(apArticle != null){
            SearchArticleVo searchArticleVo = new SearchArticleVo();
            //数据的拷贝
            BeanUtils.copyProperties(apArticle,searchArticleVo);

            //其他数据
            searchArticleVo.setLayout(apArticle.getLayout().intValue());

            kafkaTemplate.send("search-topic",JSON.toJSONString(searchArticleVo));
        }
    }
}
